<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Company;
use App\Model\Comcheckdetail;
use App\Model\Area;
use App\Model\Comcheck;
use App\Model\Areapoint;
use App\Model\SuperviseCompany;
use App\Model\Comcheckagain;
use App\Model\Checkcategory;
use App\Model\Stock;
use App\Model\MaterialList;

class MapController extends Controller
{
    public function index()
    {
    	/*获取企业分类*/
    	$categories = Checkcategory::orderBy("sort","asc")->get();
    	$categories = json_decode(json_encode($categories), true);

    	return view('admin.map.index',[
    		'categories' => $categories
    	]);
    }
    public function indexall()
    {
        /*获取企业分类*/
        $categories = Checkcategory::orderBy("sort","asc")->get();
        $categories = json_decode(json_encode($categories), true);

        return view('admin.map.indexall',[
            'categories' => $categories
        ]);
    }
    public function index3()
    {
        /*获取企业分类*/
        $categories = Checkcategory::orderBy("sort","asc")->get();
        $categories = json_decode(json_encode($categories), true);

        return view('admin.map.index3',[
            'categories' => $categories
        ]);
    }
    public function index2()
    {
        /*获取企业分类*/
        $categories = Checkcategory::orderBy("sort","asc")->get();
        $categories = json_decode(json_encode($categories), true);

        return view('admin.map.index2',[
            'categories' => $categories
        ]);
    }
    /*获取所有公司  物资查询用 */
    public function getAllCompany3(Request $request)
    {
        $name = $request->input("name","");

        if($name){
            $list= MaterialList::with('goods')->orderby('id','asc')->where('name','like','%'.$name.'%');
            $materids=MaterialList::where('name','like','%'.$name.'%')->pluck('id');
            $coms = Company::whereHas('stocks',function($query)use($materids){
                $query->whereIn('id',$materids)->where('warehouse','>',0);
            });
        }else{
            $list= MaterialList::with('goods')->orderby('id','asc');
            $coms = Company::whereHas('stocks',function($query){
                $query->where('warehouse','>',0);
            });
        }
        $dataarr=$list->get()->toArray();
        $comarr=$coms->where('lng','<>','')->whereNotNull('lng')->get()->toArray();
        foreach($comarr as $k=>&$v) {
            if($v['lat']!=null&&$v['lng']!=null) {
                $encrypt = bd_encrypt($v['lng'], $v['lat']);
                $v['lng'] = $encrypt['bd_lon'];
                $v['lat'] = $encrypt['bd_lat'];
            }
        }
        return response()->json(compact('dataarr','comarr'));
    }
    /*获取所有公司*/
    public function getAllCompany(Request $request)
    {
    	$name = $request->input("name","");
    	$category = $request->input("category","11");
    	/*获取公司信息*/
    	$companyObj = new Company();
    	$companies = $companyObj->getCompanyList($name,$category);
    	$companies = json_decode(json_encode($companies), true);
    	/*获取初查一般隐患*/
    	$danger_where = array();
    	$danger_where[] = array('status',2);
    	$dangerdetails = Comcheckdetail::where($danger_where)->select(array('id','checkId'))->groupBy('checkId')->get();
    	$dangerdetails = json_decode(json_encode($dangerdetails), true);
    	$dangerdetails = array_column($dangerdetails, null, 'checkId');
    	/*获取初查重大隐患*/
    	$major_danger_where = array();
    	$major_danger_where[] = array('isfire','<>',0);
    	$majordangerdetails = Comcheckdetail::where($major_danger_where)->select(array('id','checkId'))->groupBy('checkId')->get();
    	$majordangerdetails = json_decode(json_encode($majordangerdetails), true);
    	$majordangerdetails = array_column($majordangerdetails, null, 'checkId');
    	/*获取复查整改情况*/
    	$checkagain_obj = new Comcheckagain();
    	$checkagain_dangers = $checkagain_obj->getDangerNum();
    	$checkagain_dangers = json_decode(json_encode($checkagain_dangers), true);
    	$checkagain_dangers = array_column($checkagain_dangers, null, 'checkId');

    	$returnData = array();
    	$returnData = $companies;
    	foreach($returnData as $k=>$v) {
    		if($v['lat']!=null&&$v['lng']!=null) {
    			$encrypt = bd_encrypt($v['lng'], $v['lat']);
    			$returnData[$k]['lng'] = $encrypt['bd_lon'];
    			$returnData[$k]['lat'] = $encrypt['bd_lat'];
    		}
    		if(isset($majordangerdetails[$v['checkId']])) {/*重大隐患*/
    			$returnData[$k]['has_danger'] = 2;
    		} else if(isset($checkagain_dangers[$v['checkId']])&&$checkagain_dangers[$v['checkId']]['num']==0) {/*复查是否已经整改*/
    			$returnData[$k]['has_danger'] = 0;
    		} else if(isset($dangerdetails[$v['checkId']])){/*一般隐患*/
    			$returnData[$k]['has_danger'] = 1;
    		} else {
    			$returnData[$k]['has_danger'] = 0;
    		}
    	}
    	$returnData = unsetNull($returnData);
    	return response()->json($returnData);
    }
    /*获取大屏展示所有公司*/
    public function daping_company(Request $request)
    {
        $name = $request->input("name","");
        $category = $request->input("category","11");
        /*获取公司信息*/
        $companyObj = new Company();
        $companies = $companyObj->getCompanyList($name,$category);
        $companies = json_decode(json_encode($companies), true);
        /*获取初查一般隐患*/
        $danger_where = array();
        $danger_where[] = array('status',2);
        $dangerdetails = Comcheckdetail::where($danger_where)->select(array('id','checkId'))->groupBy('checkId')->get();
        $dangerdetails = json_decode(json_encode($dangerdetails), true);
        $dangerdetails = array_column($dangerdetails, null, 'checkId');
        /*获取初查重大隐患*/
        $major_danger_where = array();
        $major_danger_where[] = array('isfire','<>',0);
        $majordangerdetails = Comcheckdetail::where($major_danger_where)->select(array('id','checkId'))->groupBy('checkId')->get();
        $majordangerdetails = json_decode(json_encode($majordangerdetails), true);
        $majordangerdetails = array_column($majordangerdetails, null, 'checkId');
        /*获取复查整改情况*/
        $checkagain_obj = new Comcheckagain();
        $checkagain_dangers = $checkagain_obj->getDangerNum();
        $checkagain_dangers = json_decode(json_encode($checkagain_dangers), true);
        $checkagain_dangers = array_column($checkagain_dangers, null, 'checkId');

        $returnData = array();
        $returnData = $companies;
        foreach($returnData as $k=>$v) {
            if($v['lat']!=null&&$v['lng']!=null) {
                $encrypt = bd_encrypt($v['lng'], $v['lat']);
                $returnData[$k]['lng'] = $encrypt['bd_lon'];
                $returnData[$k]['lat'] = $encrypt['bd_lat'];
            }
            if(isset($majordangerdetails[$v['checkId']])) {/*重大隐患*/
                $returnData[$k]['has_danger'] = 2;
            } else if(isset($checkagain_dangers[$v['checkId']])&&$checkagain_dangers[$v['checkId']]['num']==0) {/*复查是否已经整改*/
                $returnData[$k]['has_danger'] = 0;
            } else if(isset($dangerdetails[$v['checkId']])){/*一般隐患*/
                $returnData[$k]['has_danger'] = 1;
            } else {
                $returnData[$k]['has_danger'] = 0;
            }
        }
        $returnData = unsetNull($returnData);
        $data=[
            'code'=>1,
            'msg'=>'请求成功',
            'data'=>$returnData
        ];
        return response()->json($data);
    }
    /*获取公司信息*/
    public function getCompany(Request $request)
    {
    	$id = $request->input("id");
    	$filed = array("id","comName","leader","leaderPhone","workersNum","space","province","city","district","street","address");
    	$company = Company::where('id', $id)->select($filed)->first()->toArray();
		$province_arr = Area::where('id', $company['province'])->first()->toArray();
		$address_where = array();
		$address_where[] = array('node', 'like', $company['province'].'%');
		$child_address_arr = Area::where($address_where)->get()->toArray();
		$child_address_arr = array_column($child_address_arr, null,"id");
    	$returnData = array();
    	$returnData = $company;
    	$returnData['address'] = $province_arr['name'].$child_address_arr[$company['city']]['name'].$child_address_arr[$company['district']]['name'].$child_address_arr[$company['street']]['name'].$company['address'];
    	$returnData = unsetNull($returnData);

    	return response()->json($returnData);
    }

    /*检查记录*/
    public function getCheckLogs(Request $request)
    {
    	$name = $request->input("name");
    	$checkObj = new Comcheck();
    	$checks = $checkObj->getChecks($name);
    	$checks = json_decode(json_encode($checks), true);

    	$returnData = array();
    	$returnData = $checks;
    	foreach($returnData as $k=>$v) {
    		if(!empty($v['updated'])) {
    			$returnData[$k]['checkTime'] = date('Y-m-d', $v['updated']);
    		} else {
    			$returnData[$k]['checkTime'] = mb_substr($v['created'], 0, 10, 'utf-8');
    		}
    	}

    	return response()->json($returnData);
    }
    
    /*获取所有社区隐患数量*/
    public function getDangersNum()
    {
    	$areapoint_obj = new Areapoint();
    	$area_points = $areapoint_obj->getAll();
    	$area_points = json_decode(json_encode($area_points), true);
    	/*获取所有隐患数量*/
    	$checkdetail_obj = new Comcheckdetail();
    	$dangers = $checkdetail_obj->getStaticByCommunity();
    	$dangers = json_decode(json_encode($dangers), true);
    	$dangers = array_column($dangers, null, 'community');
		foreach ($area_points as $k => $v) {
			$area_points[$k]['danger_num'] = 0;
			if(isset($dangers[$v['area_id']])) {
				$area_points[$k]['danger_num'] += $dangers[$v['area_id']]['num'];
			}
		}

    	$returnData = array();
  		$returnData = $area_points;
    	return response()->json($returnData);
    }

    /*获取所有社区的企业数量*/
    public function getCompanyNum()
    {
    	$areapoint_obj = new Areapoint();
    	$community = $areapoint_obj->getAll();
    	$community = json_decode(json_encode($community), true);

    	$company_obj = new Company();
    	$company = $company_obj->getCompanyNumByCommunity();
    	$company = json_decode(json_encode($company), true);
    	$company = array_column($company, null, 'community');
    	foreach ($community as $key => $value) {
    		$community[$key]['num'] = 0;
    		if(isset($company[$value['area_id']])) {
    			$community[$key]['num'] += $company[$value['area_id']]['num'];
    		}
    	}

    	return response()->json($community);
    }
    /*获取企业监管数量*/
    public function getSuperviseCompanyNum()
    {
    	$supcom_obj = new SuperviseCompany();
    	$res = $supcom_obj->getNumByArea();
    	$res = json_decode(json_encode($res), true);

    	$returnData = array();
    	$returnData = $res;

    	return response()->json($returnData);
    }

    /*获取所有社区*/
    public function getCommunity()
    {
    	$areapoint_obj = new Areapoint();
    	$community = $areapoint_obj->getAll();
    	$community = json_decode(json_encode($community), true);

    	return response()->json($community);
    }
}
